{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "Tensors与Numpy中的 ndarrays类似，但是在PyTorch中 Tensors 可以使用GPU进行计算."
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "source": [
    "from __future__ import print_function\n",
    "import torch"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "source": [
    "x = torch.empty(5,3) #创建5x3矩阵，未初始化\n",
    "print(x)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "tensor([[2.5352e-24, 4.5572e-41, 2.5582e-24],\n",
      "        [4.5572e-41, 2.7065e-24, 4.5572e-41],\n",
      "        [2.5575e-24, 4.5572e-41, 2.5960e-20],\n",
      "        [4.5572e-41, 2.6583e-24, 4.5572e-41],\n",
      "        [2.4615e-24, 4.5572e-41, 2.4615e-24]])\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "source": [
    "x = torch.rand(5,3)\n",
    "print(x)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "tensor([[0.0611, 0.7732, 0.0366],\n",
      "        [0.4170, 0.2519, 0.0286],\n",
      "        [0.5893, 0.3385, 0.0689],\n",
      "        [0.9233, 0.5677, 0.5427],\n",
      "        [0.3309, 0.6175, 0.1503]])\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "source": [
    "x = torch.zeros(5,3,dtype=torch.long)\n",
    "print(x)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "tensor([[0, 0, 0],\n",
      "        [0, 0, 0],\n",
      "        [0, 0, 0],\n",
      "        [0, 0, 0],\n",
      "        [0, 0, 0]])\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "source": [
    "x = torch.Tensor([5.5,3])\n",
    "print(x)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "tensor([5.5000, 3.0000])\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "source": [
    "x = x.new_ones(5,3,dtype=torch.double)\n",
    "print(x)\n",
    "x = torch.randn_like(x, dtype=torch.float)\n",
    "print(x)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "tensor([[1., 1., 1.],\n",
      "        [1., 1., 1.],\n",
      "        [1., 1., 1.],\n",
      "        [1., 1., 1.],\n",
      "        [1., 1., 1.]], dtype=torch.float64)\n",
      "tensor([[-0.3934,  0.6098, -0.4876],\n",
      "        [-0.2583,  0.5692,  2.0055],\n",
      "        [-0.0208, -0.4151,  0.9783],\n",
      "        [ 0.0844,  0.5623,  0.0948],\n",
      "        [-0.4600, -1.8147, -0.7021]])\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "获取张量的维度，size.与numpy的shape类似"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "source": [
    "print(x.size()) #返回的是tuple类型"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "torch.Size([5, 3])\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "source": [
    "y = torch.rand(5,3)\n",
    "print(x+y)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "tensor([[ 0.5058,  1.5021, -0.0274],\n",
      "        [ 0.1079,  1.2722,  2.3938],\n",
      "        [ 0.3842,  0.0554,  1.0339],\n",
      "        [ 0.3852,  0.7640,  0.7667],\n",
      "        [ 0.4256, -1.0766, -0.2268]])\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "source": [
    "print(torch.add(x,y))"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "tensor([[ 0.5058,  1.5021, -0.0274],\n",
      "        [ 0.1079,  1.2722,  2.3938],\n",
      "        [ 0.3842,  0.0554,  1.0339],\n",
      "        [ 0.3852,  0.7640,  0.7667],\n",
      "        [ 0.4256, -1.0766, -0.2268]])\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "source": [
    "result = torch.empty(5,3)\n",
    "torch.add(x,y,out = result)\n",
    "print(result)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "tensor([[ 0.5058,  1.5021, -0.0274],\n",
      "        [ 0.1079,  1.2722,  2.3938],\n",
      "        [ 0.3842,  0.0554,  1.0339],\n",
      "        [ 0.3852,  0.7640,  0.7667],\n",
      "        [ 0.4256, -1.0766, -0.2268]])\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "source": [
    "y.add_(x)  #任何 以``_`` 结尾的操作都会用结果替换原变量. 例如: ``x.copy_(y)``, ``x.t_()``, 都会改变 ``x``.\n",
    "print(y)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "tensor([[ 0.5058,  1.5021, -0.0274],\n",
      "        [ 0.1079,  1.2722,  2.3938],\n",
      "        [ 0.3842,  0.0554,  1.0339],\n",
      "        [ 0.3852,  0.7640,  0.7667],\n",
      "        [ 0.4256, -1.0766, -0.2268]])\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "source": [
    "print(x[:,1]) #你可以使用与NumPy索引方式相同的操作来进行对张量的操作"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "tensor([ 0.6098,  0.5692, -0.4151,  0.5623, -1.8147])\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "torch.view: 可以改变张量的维度和大小  \n",
    "torch.view 与Numpy的reshape类似  "
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "source": [
    "x = torch.randn(4,4)\n",
    "y = x.view(16)\n",
    "z = x.view(-1,8)\n",
    "print(x)\n",
    "print(y)\n",
    "print(z)\n",
    "print(x.size(), y.size(), z.size())"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "tensor([[ 0.3140,  0.8545, -0.8926,  0.4154],\n",
      "        [ 0.4097,  1.7466,  1.0043,  0.0609],\n",
      "        [ 1.1373,  1.3669, -1.5780, -0.7181],\n",
      "        [-1.3497,  0.0523,  1.0688, -0.2943]])\n",
      "tensor([ 0.3140,  0.8545, -0.8926,  0.4154,  0.4097,  1.7466,  1.0043,  0.0609,\n",
      "         1.1373,  1.3669, -1.5780, -0.7181, -1.3497,  0.0523,  1.0688, -0.2943])\n",
      "tensor([[ 0.3140,  0.8545, -0.8926,  0.4154,  0.4097,  1.7466,  1.0043,  0.0609],\n",
      "        [ 1.1373,  1.3669, -1.5780, -0.7181, -1.3497,  0.0523,  1.0688, -0.2943]])\n",
      "torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "source": [
    "x = torch.randn(1)  #如果你有只有一个元素的张量，使用.item()来得到Python数据类型的数值\n",
    "print(x)\n",
    "print(x.item())"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "tensor([-1.9627])\n",
      "-1.9626668691635132\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "NumPy 转换¶\n",
    "将一个Torch Tensor转换为NumPy数组是一件轻松的事，反之亦然。\n",
    "\n",
    "Torch Tensor与NumPy数组共享底层内存地址，修改一个会导致另一个的变化。\n",
    "\n",
    "将一个Torch Tensor转换为NumPy数组"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "source": [
    "a = torch.ones(5)\n",
    "print(a)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "tensor([1., 1., 1., 1., 1.])\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "source": [
    "b = a.numpy()\n",
    "print(b)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "[1. 1. 1. 1. 1.]\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "source": [
    "a.add_(1)\n",
    "print(a)\n",
    "print(b)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "tensor([2., 2., 2., 2., 2.])\n",
      "[2. 2. 2. 2. 2.]\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "source": [
    "import numpy as np\n",
    "a = np.ones(5)  #numpy默认为double float64\n",
    "b = torch.from_numpy(a)  #所有的 Tensor 类型默认都是基于CPU， CharTensor 类型不支持到 NumPy 的转换.\n",
    "np.add(a, 1, out = a) \n",
    "print(a)\n",
    "print(b)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "[2. 2. 2. 2. 2.]\n",
      "tensor([2., 2., 2., 2., 2.], dtype=torch.float64)\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "source": [
    "if torch.cuda.is_available():\n",
    "    device = torch.device(\"cuda\")\n",
    "    y = torch.ones_like(x, device=device)\n",
    "    x = x.to(device)\n",
    "    z = x + y\n",
    "    print(z)\n",
    "    print(z.to(\"cpu\", torch.double))"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "tensor([-0.9627], device='cuda:0')\n",
      "tensor([-0.9627], dtype=torch.float64)\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "PyTorch 基础 : 张量"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "source": [
    "import torch\n",
    "import numpy as np\n",
    "torch.__version__"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "'1.9.1+cu111'"
      ]
     },
     "metadata": {},
     "execution_count": 22
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "source": [
    "x = torch.rand(2,3)\n",
    "print(x.shape) # 可以使用与numpy相同的shape属性查看\n",
    "print(x.size()) # 也可以使用size()函数，返回的结果都是相同的"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "torch.Size([2, 3])\n",
      "torch.Size([2, 3])\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "张量（Tensor）是一个定义在一些向量空间和一些对偶空间的笛卡儿积上的多重线性映射，其坐标是|n|维空间内，有|n|个分量的一种量， 其中每个分量都是坐标的函数， 而在坐标变换时，这些分量也依照某些规则作线性变换。\n",
    "在同构的意义下，第零阶张量 （r = 0） 为标量 （Scalar），第一阶张量 （r = 1） 为向量 （Vector）， 第二阶张量 （r = 2） 则成为矩阵 （Matrix），第三阶以上的统称为多维张量。"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "source": [
    "scalar = torch.tensor(3.4)\n",
    "print(scalar)\n",
    "scalar.size()\n",
    "print(scalar.item())  #对于标量，我们可以直接使用 .item() 从中取出其对应的python对象的数值\n",
    "tensor = torch.tensor([3.4])\n",
    "print(tensor)\n",
    "tensor.size()\n",
    "print(tensor.item()) #特别的：如果张量中只有一个元素的tensor也可以调用tensor.item方法"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "tensor(3.4000)\n",
      "3.4000000953674316\n",
      "tensor([3.4000])\n",
      "3.4000000953674316\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "\n",
    "基本类型¶\n",
    "Tensor的基本数据类型有五种：\n",
    "\n",
    "32位浮点型：torch.FloatTensor。 (默认)\n",
    "64位整型：torch.LongTensor。\n",
    "32位整型：torch.IntTensor。\n",
    "16位整型：torch.ShortTensor。\n",
    "64位浮点型：torch.DoubleTensor。\n",
    "除以上数字类型外，还有 byte和chart型"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "source": [
    "long = tensor.long()\n",
    "long"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "tensor([3])"
      ]
     },
     "metadata": {},
     "execution_count": 30
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "source": [
    "half = tensor.half()\n",
    "half"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "tensor([3.4004], dtype=torch.float16)"
      ]
     },
     "metadata": {},
     "execution_count": 31
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "source": [
    "int_t = tensor.int()\n",
    "int_t"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "tensor([3], dtype=torch.int32)"
      ]
     },
     "metadata": {},
     "execution_count": 32
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "source": [
    "flo = tensor.float()\n",
    "flo"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "tensor([3.4000])"
      ]
     },
     "metadata": {},
     "execution_count": 33
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "source": [
    "short = tensor.short()\n",
    "short"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "tensor([3], dtype=torch.int16)"
      ]
     },
     "metadata": {},
     "execution_count": 34
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "source": [
    "ch = tensor.char()\n",
    "ch"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "tensor([3], dtype=torch.int8)"
      ]
     },
     "metadata": {},
     "execution_count": 35
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "source": [
    "bt = tensor.byte()\n",
    "bt"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "tensor([3], dtype=torch.uint8)"
      ]
     },
     "metadata": {},
     "execution_count": 36
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "Numpy转换¶\n",
    "使用numpy方法将Tensor转为ndarray"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "source": [
    "a = torch.randn((3,2))\n",
    "numpy_a = a.numpy()\n",
    "print(numpy_a)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "[[ 0.27408898  0.2759553 ]\n",
      " [-1.2953687   1.4922767 ]\n",
      " [-0.9517581  -0.9991583 ]]\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "source": [
    "torch_a = torch.from_numpy(numpy_a)\n",
    "torch_a  #Tensor和numpy对象共享内存，所以他们之间的转换很快，而且几乎不会消耗什么资源。但这也意味着，如果其中一个变了，另外一个也会随之改变。"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "tensor([[ 0.2741,  0.2760],\n",
       "        [-1.2954,  1.4923],\n",
       "        [-0.9518, -0.9992]])"
      ]
     },
     "metadata": {},
     "execution_count": 38
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "设备间转换¶\n",
    "一般情况下可以使用.cuda方法将tensor移动到gpu，这步操作需要cuda设备支持"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "source": [
    "cpu_a = torch.rand(4,3)\n",
    "cpu_a.type()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "'torch.FloatTensor'"
      ]
     },
     "metadata": {},
     "execution_count": 39
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "source": [
    "gpu_a = cpu_a.cuda()\n",
    "gpu_a.type()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "'torch.cuda.FloatTensor'"
      ]
     },
     "metadata": {},
     "execution_count": 40
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "source": [
    "cpu_b = gpu_a.cpu()\n",
    "cpu_b.type()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "'torch.FloatTensor'"
      ]
     },
     "metadata": {},
     "execution_count": 41
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "source": [
    "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\") \n",
    "print(device)\n",
    "gpu_b = cpu_b.to(device)  #如果我们有多GPU的情况，可以使用to方法来确定使用那个设备，这里只做个简单的实例：\n",
    "gpu_b.type()"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "cuda\n"
     ]
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "'torch.cuda.FloatTensor'"
      ]
     },
     "metadata": {},
     "execution_count": 42
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "source": [
    "rnd = torch.rand(5,3)  #初始化\n",
    "rnd"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "tensor([[0.2559, 0.0521, 0.6834],\n",
       "        [0.9727, 0.4557, 0.7227],\n",
       "        [0.3236, 0.5232, 0.1807],\n",
       "        [0.5426, 0.3698, 0.1594],\n",
       "        [0.6820, 0.9300, 0.2277]])"
      ]
     },
     "metadata": {},
     "execution_count": 43
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "source": [
    "one = torch.ones(2,2)\n",
    "one"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "tensor([[1., 1.],\n",
       "        [1., 1.]])"
      ]
     },
     "metadata": {},
     "execution_count": 44
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "source": [
    "zero = torch.zeros(2,2)\n",
    "zero"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "tensor([[0., 0.],\n",
       "        [0., 0.]])"
      ]
     },
     "metadata": {},
     "execution_count": 45
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "source": [
    "eye = torch.eye(2,2)\n",
    "eye"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "tensor([[1., 0.],\n",
       "        [0., 1.]])"
      ]
     },
     "metadata": {},
     "execution_count": 46
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "\n",
    "常用方法¶\n",
    "PyTorch中对张量的操作api 和 NumPy 非常相似，如果熟悉 NumPy 中的操作，那么他们二者基本是一致的："
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "source": [
    "x = torch.rand(3,3)\n",
    "print(x)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "tensor([[0.7185, 0.7974, 0.3302],\n",
      "        [0.3944, 0.1928, 0.5375],\n",
      "        [0.1060, 0.6769, 0.6250]])\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "source": [
    "max_value, max_idx = torch.max(x,dim=1)\n",
    "print(max_value, max_idx)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "tensor([0.7974, 0.5375, 0.6769]) tensor([1, 2, 1])\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "source": [
    "sum_x = torch.sum(x,dim=1)\n",
    "print(sum_x)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "tensor([1.8461, 1.1247, 1.4079])\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "source": [
    "y = torch.randn(3,3)\n",
    "z = x+y"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  }
 ],
 "metadata": {
  "orig_nbformat": 4,
  "language_info": {
   "name": "python",
   "version": "3.8.10",
   "mimetype": "text/x-python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python",
   "file_extension": ".py"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.8.10 64-bit ('d2l-zh': conda)"
  },
  "interpreter": {
   "hash": "0bedf3452ed48e38a2b75ff9f66ce8ae9ae2e92e8665cb618ee0797d2f46b9e5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}